package cn.tedu.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class UserLogoutServlet
 */
public class UserLogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserLogoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//系统的登出操作,要做一些相关的清除操作,比如清除session对象,或session对象中的数据,
		HttpSession hahaha=request.getSession();
		//1.session对象保留,但清除的是session对象中的数据(不建议);
		//hahaha.removeAttribute("code");
		//hahaha.removeAttribute("user_name");
		//2.session对象都给销毁了,那么session对象中的数据自然也销毁了
		hahaha.invalidate();//推荐
		
		
		
		//清除cookie
		//方案一
		/*Cookie[] cookies=request.getCookies();
		if(cookies!=null) {
			for(Cookie c:cookies) {
				c.setMaxAge(0);//销毁c这个cookie
				c.setPath(request.getContextPath()+"/");
				response.addCookie(c);
			}
		}
		*/
		//方案二  删除指定的cookie 
		Cookie cookie=new Cookie("autologin","test");
		cookie.setMaxAge(0);
		cookie.setPath(request.getContextPath()+"/");
		response.addCookie(cookie);
		
		Cookie cookie1=new Cookie("remname","test");
		cookie1.setMaxAge(0);
		cookie1.setPath(request.getContextPath()+"/");
		response.addCookie(cookie1);
		
		response.sendRedirect("login.jsp");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
